package cn.xfdzx.android.apps.shop.activity.ruzhu;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.content.res.ColorStateList;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.material.tabs.TabLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.HomeVPAdapter;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.evenbus.SearchGoodsContentMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateCancelOrderMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateShopOrderMessage;
import cn.xfdzx.android.apps.shop.fragment.ShopInOrderAfterSalesFragment;
import cn.xfdzx.android.apps.shop.fragment.ShopInOrderFragment;
import cn.xfdzx.android.apps.shop.fragment.ShopInPiFaOrderAfterSalesFragment;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;

public class OrderManagementActivity extends BaseMvpActivity {

    @BindView(R.id.tv_goods_search)
    TextView tvGoodsSearch;
  @BindView(R.id.iv_search_delete)
    ImageView iv_search_delete;
    @BindView(R.id.et_goods_search)
    EditText et_goods_search;//搜索商品
    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.tl_fragment_all)
    TabLayout mTabLayout;
    @BindView(R.id.vp_fragment_all)
    ViewPager vpFragmentAll;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    private String[] title = {"全部", "待支付", "待发货", "待收货", "已完成","售后"};
    private RelativeLayout.LayoutParams item_bar;
    private HomeVPAdapter homeVPAdapter;
    private List<Fragment> mFragments;
    private String type;
    private int currentPosition = 0;


    @Override
    public int getLayoutId() {
        return R.layout.activity_order_management;
    }



    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXX(UpdateCancelOrderMessage messageEvent) {
        if (vpFragmentAll.getCurrentItem() == 0) {
            EventBus.getDefault().post(new UpdateShopOrderMessage("all"));
        } else if (vpFragmentAll.getCurrentItem() == 1) {
            EventBus.getDefault().post(new UpdateShopOrderMessage("0"));
        } else if (vpFragmentAll.getCurrentItem() == 2) {
            EventBus.getDefault().post(new UpdateShopOrderMessage("1"));
        } else if (vpFragmentAll.getCurrentItem() == 3) {
            EventBus.getDefault().post(new UpdateShopOrderMessage("2"));
        } else if (vpFragmentAll.getCurrentItem() == 4) {
            EventBus.getDefault().post(new UpdateShopOrderMessage("3"));
        } else if (vpFragmentAll.getCurrentItem() == 5) {
            EventBus.getDefault().post(new UpdateShopOrderMessage("sale"));
        }
        currentPosition= vpFragmentAll.getCurrentItem();
    }
    @Override
    public void initView() {
        initHigh();
        EventBus.getDefault().register(this);
        type = getIntent().getStringExtra("type");
        mFragments = new ArrayList<>();
        mFragments.add(ShopInOrderFragment.newInstance("all", type));//
        mFragments.add(ShopInOrderFragment.newInstance("0", type));//
        mFragments.add(ShopInOrderFragment.newInstance("1", type));//
        mFragments.add(ShopInOrderFragment.newInstance("2", type));//
        mFragments.add(ShopInOrderFragment.newInstance("3", type));//
        if (type.equals("0")){
            mFragments.add(ShopInOrderAfterSalesFragment.newInstance("sale", type));//
        }else {
            mFragments.add(ShopInPiFaOrderAfterSalesFragment.newInstance("sale", type));//
        }

        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setTabRippleColor(ColorStateList.valueOf(this.getResources().getColor(R.color.picture_color_transparent)));
        for (String tab : title) {
            mTabLayout.addTab(mTabLayout.newTab().setText(tab));
        }
        if (type.equals("0")){
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_333333), getResources().getColor(R.color.color_FB9129));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_FB9129));
        }else {
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_333333), getResources().getColor(R.color.color_06B359));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_06B359));
        }
        homeVPAdapter = new HomeVPAdapter(getSupportFragmentManager(), title, mFragments);
        vpFragmentAll.setAdapter(homeVPAdapter);
        vpFragmentAll.setOffscreenPageLimit(4);

        mTabLayout.setupWithViewPager(vpFragmentAll);

        initTextWacherListener();
        iv_search_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_goods_search.getText().clear();
                EventBus.getDefault().post(new SearchGoodsContentMessage("",0,currentPosition));
            }
        });
        tvGoodsSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new SearchGoodsContentMessage(et_goods_search.getText().toString().trim(),0,currentPosition));
            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        vpFragmentAll.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("all"));
                } else if (position == 1) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("0"));
                } else if (position == 2) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("1"));
                } else if (position == 3) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("2"));
                } else if (position == 4) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("3"));
                }else if (position == 5) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("sale"));
                }
                currentPosition= position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        initOnPreDrawListener();
        vpFragmentAll.setCurrentItem(getIntent().getIntExtra("position", 0));
    }

    private void initTextWacherListener() {
        et_goods_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable .toString().isEmpty()) {
                    iv_search_delete.setVisibility(View.INVISIBLE);
                } else {
                    iv_search_delete.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void initOnPreDrawListener() {
        final ViewTreeObserver viewTreeObserver = this.getWindow().getDecorView().getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (vpFragmentAll.getCurrentItem() == 0) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("all"));
                } else if (vpFragmentAll.getCurrentItem() == 1) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("0"));
                } else if (vpFragmentAll.getCurrentItem() == 2) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("1"));
                } else if (vpFragmentAll.getCurrentItem() == 3) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("2"));
                } else if (vpFragmentAll.getCurrentItem() == 4) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("3"));
                }else if (vpFragmentAll.getCurrentItem() == 5) {
                    EventBus.getDefault().post(new UpdateShopOrderMessage("sale"));
                }
                currentPosition= vpFragmentAll.getCurrentItem();
                // 移除OnPreDrawListener事件监听
                getWindow().getDecorView().getViewTreeObserver().removeOnPreDrawListener(this);

                return true;
            }
        });
    }
    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }
}
